package cn.edu.zut.hhr.utils;

import io.jsonwebtoken.CompressionCodecs;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author HHR
 * 通过jwt生成token
 */
@Component
public class TokenUtil {

    //过期时间一天吧
    public static final long tokenExpiration = 24*60*60*1000;
    public static final String TOKEN_KEY = "hhr12138";

    public static String createToken(String username){
        String token = Jwts.builder().setSubject(username)
                .setExpiration(new Date(System.currentTimeMillis() + tokenExpiration))
                .signWith(SignatureAlgorithm.HS512,TOKEN_KEY)
                .compressWith(CompressionCodecs.GZIP)
                .compact();
        return token;
    }

    public static String getUserInfoFromToken(String token){
        return Jwts.parser().setSigningKey(TOKEN_KEY).parseClaimsJws(token).getBody().getSubject();
    }


}
